package com.wang.back.servlet;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.security.auth.message.callback.PrivateKeyCallback.Request;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.wang.common.pojo.InfoEnum;
import com.wang.common.servlet.BaseServlet;
import com.wang.common.util.ExecutorsUtil;
import com.wang.common.util.PageInfoUtil;
import com.wang.users.pojo.AStatRes;
import com.wang.users.service.IAuthDbService;
import com.wang.users.service.IUsersDbService;
import com.wang.users.service.IUsersOperService;
import com.wang.users.service.impl.AuthDbServiceImpl;
import com.wang.users.service.impl.UsersDbServiceImpl;
import com.wang.users.service.impl.UsersOperServiceImpl;
import com.wang.users.util.UsersCallableUtil;

/**
 * 登陆后访问的页面
 * 只是管理员访问的页面
 * @author Zjx
 */
@WebServlet(urlPatterns = "/back/StatBackServlet")
@MultipartConfig
public class StatBackServlet extends BaseServlet
{
	private IAuthDbService authDbService = new AuthDbServiceImpl();
	private IUsersDbService usersDbService = new UsersDbServiceImpl();
	private IUsersOperService usersOperService = new UsersOperServiceImpl();
	
	@Override
	public void init(ServletConfig config) throws ServletException
	{
		/**
		 * instanceof 
		 */
		if(this.usersDbService instanceof UsersDbServiceImpl)
		{
			UsersDbServiceImpl usersDbServiceImpl = (UsersDbServiceImpl) usersDbService ; 
			usersDbServiceImpl.setAuthDbService(authDbService);
		}
		
		if(this.authDbService instanceof AuthDbServiceImpl)
		{
			AuthDbServiceImpl authDbServiceImpl = (AuthDbServiceImpl) authDbService ; 
			authDbServiceImpl.setUsersDbService(usersDbService);
		}
	}
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String method = request.getParameter("method");
		if("logStatList".equalsIgnoreCase(method))
		{
			/* 打开登陆页面 */
			logStatList(request,response);
		}else if("loggStatList".equalsIgnoreCase(method))
		{
			/* 打开登陆页面 */
			statResInsert(request,response);
		}else if("logggStatList".equalsIgnoreCase(method))
		{
			/*打开收动提交页面*/
			statResInsertSubmit(request,response);
		}
		else
		{
			this.blank(request, response);
		}
	}

	/**
	 * 登陆后首页
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void logStatList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		
		PageInfoUtil pageInfoUtil = this.proccedPageInfoUtil(request);
		Map<String,Object> condMap = this.proccedSearchCond(request);
		condMap.put("operType", "dataStat");
		/* 直接查询结果表 */
		List<AStatRes> list = this.usersDbService.findCondListStatResService(pageInfoUtil, condMap);
		request.setAttribute("list", list);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		/* 服务器端跳转 */
		this.serverRedirect(request, response, "/back/stat/logStatList");
	}
	/**
	 * 手动计算，打开添加页面
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void statResInsert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/*服务端跳转*/
		this.serverRedirect(request, response, "back/stat/statResInsert");
	}
	/**
	 * 手动计算，打开提交页面
	 */
	private void  statResInsertSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		JSONObject resultJSON = new JSONObject();
		/*接收时间*/
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");
		if(st == null)
		{
			st = "";
		}
		if(ed == null)
		{
			ed = "";
		}
		Date stDate = null ;
		Date edDate = null ;
		
		if(!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			/* 转换时间 */
			stDate = this.dateUtil.strToDate(st);
			edDate = this.dateUtil.strToDate(ed);
			
			UsersCallableUtil usersCallableUtil = new UsersCallableUtil();
			/*设置值*/
			usersCallableUtil.setUsersOperService(this.usersOperService);
			Map<String,Object>paramsMap = new HashMap<String,Object>();
			paramsMap.put("stDate",stDate);
			paramsMap.put("edDate",edDate);
			
			/*设置值*/
			usersCallableUtil.setParamsMap(paramsMap);
			usersCallableUtil.setOperType("statResBatchInsert");
			/*启动线程*/
			ExecutorsUtil.submit(usersCallableUtil);	
			info = "线程已经启动,请稍后看结果" ; 
		}else 
		{
			info = "请输入时间";
		}
		resultJSON.put("info", info);
		resultJSON.put("code", InfoEnum.INFO_SUCCESS.getCode());
		/*服务端跳转*/
		this.printStr(request, response, resultJSON.toJSONString());
		//return resultJSON.toJSONString() ;
	}
}
